package com.yj.suncity.user;

import java.util.List;

import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yj.suncity.R;
import com.yj.suncity.activity.shouye.SeeAdvDetailActivity;
import com.yj.suncity.activity.shouye.SeeAdvListActivity;
import com.yj.suncity.activity.shouye.bean.SuperadvListBean;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.ThreadUtils;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.async.ThreadUtils.OnResultListener;
import com.yj.suncity.constant.ShareCon;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.user.bean.ShopCateBean;
import com.yj.suncity.user.bean.ShopCateListBean;
import com.yj.suncity.utis.SharedPreferencesUtil;
import com.yj.suncity.utis.ShopCateManager;
import com.yj.suncity.utis.Utils;
import com.yj.suncity.viewpager.ViewPagerScrollSuperadv;
import com.yj.suncity.viewpager.ViewPagerScrollSuperadv.OnPagerClick;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShopCateActivity extends Activity {

	private Context context;
	private ShopCateManager cateManager;
	private GridView gridView1;
	private int mWidth;
	private int flag;// 1`-首页-》分类信息
	private SharedPreferencesUtil preferencesUtil = SharedPreferencesUtil
			.getInstance();
	private ViewPager pager;
	private LinearLayout lay_view_select;
	private ViewPagerScrollSuperadv scrollSuperadv;
	private String cityId;
	private DisplayImageOptions options;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private View mInclude_view_page;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cate_grid);
		context = this;
		cateManager = ShopCateManager.getInstance();
		mWidth = getResources().getDisplayMetrics().widthPixels;
		flag = getIntent().getIntExtra("flag", -1);
		cityId = preferencesUtil.getString(context, ShareCon.CITYFILE,
				ShareCon.CITYID, "140881");
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.android_layout_detail)
				.showImageForEmptyUri(R.drawable.android_layout_detail)
				.showImageOnFail(R.drawable.android_layout_detail)
				.cacheInMemory(true).cacheOnDisc(true)
				// .displayer(new RoundedBitmapDisplayer(10))// 设置成圆角图片
				.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
				.build();
		findView();
		initData();
		if (flag == 1) {// 从首页跳转过来得加超广告请求数据
			getShopCate();
		}
	}

	private void findView() {
		gridView1 = (GridView) findViewById(R.id.gridView1);
		mInclude_view_page=findViewById(R.id.include_view_page);

		if (flag == 1) {// 从首页跳转过来得加超广告
			mInclude_view_page.setVisibility(View.VISIBLE);
			lay_view_select = (LinearLayout) findViewById(R.id.lay_view_select);
			RelativeLayout layout = (RelativeLayout) findViewById(R.id.lay_view_page);
			layout.setLayoutParams(new LinearLayout.LayoutParams(mWidth,
					(int) (mWidth / 3)));
			pager = (ViewPager) findViewById(R.id.view_pager);
		}
	}

	private void initData() {
		List<ShopCateBean> cateBeans = cateManager.getShopCate();
		if (cateBeans != null && cateBeans.size() > 0) {
			gridView1.setAdapter(new CateAdapter(cateBeans));
		} else {
			String path = URLCon.分类 + "?action=tradeinfo";
			Log.d("", path);
			AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, null,
					null, false, null, true, "正在加载......",
					new ResultCallBack() {
						@Override
						public void JSONResult(JSONObject jsonObject) {
							ShopCateListBean cateListBean = new ShopCateListBean(
									jsonObject);
							if (cateListBean.getSuccess().equals("1")) {
								cateManager.addShopCate(cateListBean.getData());
								gridView1.setAdapter(new CateAdapter(
										cateListBean.getData()));
							}
						}
					});
			asyncTaskUtil.execute(path);
		}
	}

	private void getShopCate() {
		String path = URLCon.超广广告 + "?action=superadvlist&cityid=" + cityId
				+ "&type=8";
		Log.d("", "超广广告=" + path);
		ThreadUtils threadUtils = new ThreadUtils();
		threadUtils.startThread(path, new OnResultListener() {
			@Override
			public void jsonResult(JSONObject json) {
				SuperadvListBean listBean = new SuperadvListBean(json);
				scrollSuperadv = new ViewPagerScrollSuperadv(context,
						imageLoader, options, pager, lay_view_select, listBean
								.getData(), new OnPagerClick() {
							@Override
							public void onPagerClick(String advid,
									String adv_userid) {
								Intent intent = new Intent(context,
										SeeAdvDetailActivity.class);
								intent.putExtra("adv_userid", adv_userid);
								intent.putExtra("advid", advid);
								intent.putExtra("flag", 2);
								startActivity(intent);
							}
						});

			}
		});
	}

	@Override
	protected void onResume() {
		if (scrollSuperadv != null) {
			scrollSuperadv.startTimer();
		}
		super.onResume();
	}

	@Override
	protected void onPause() {
		if (scrollSuperadv != null) {
			scrollSuperadv.stopTimer();
		}
		super.onPause();
	}

	class CateAdapter extends BaseAdapter {
		private List<ShopCateBean> cateBeans;
		private int dip_10;

		public CateAdapter(List<ShopCateBean> cateBeans) {
			super();
			this.cateBeans = cateBeans;
			dip_10 = Utils.dipTopx(context, 10);
		}

		@Override
		public int getCount() {
			return cateBeans.size();
		}

		@Override
		public Object getItem(int position) {
			return cateBeans.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.adapter_shop_cate, null);

				holder.mLay_rela_cate = (RelativeLayout) convertView
						.findViewById(R.id.lay_rela_cate);
				holder.mTxt_cate_name = (TextView) convertView
						.findViewById(R.id.txt_cate_name);
				holder.mLay_rela_cate
						.setLayoutParams(new LinearLayout.LayoutParams(
								(int) (mWidth / 3.2 - dip_10),
								(int) ((mWidth / 3.2 - dip_10) / 2)));
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final ShopCateBean helpBean = cateBeans.get(position);
			holder.mTxt_cate_name.setText(helpBean.getTrade_name());
			holder.mLay_rela_cate.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					if (flag == 1) {
						Intent intent = new Intent(context,
								SeeAdvListActivity.class);
						intent.putExtra("tradeid", helpBean.getId());
						intent.putExtra("titlename", helpBean.getTrade_name());
						intent.putExtra("flag", 2);
						startActivity(intent);
					} else {
						Intent intent = new Intent();
						intent.putExtra("cateid", helpBean.getId());
						intent.putExtra("catename", helpBean.getTrade_name());
						setResult(102, intent);
						ShopCateActivity.this.finish();
					}
				}
			});
			return convertView;
		}
	}

	class ViewHolder {

		TextView mTxt_cate_name;
		RelativeLayout mLay_rela_cate;

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;

		default:
			break;
		}

	}

}
